package org.dromara.run.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.basic.service.IMemberService;
import org.dromara.run.domain.bo.RunListBo;
import org.dromara.run.domain.bo.ScoreBo;
import org.dromara.run.domain.vo.RunListVo;
import org.dromara.run.service.IRunListService;
import org.dromara.run.service.IScoreService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.run.domain.vo.ScheduleVo;
import org.dromara.run.domain.bo.ScheduleBo;
import org.dromara.run.service.IScheduleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 阳光跑计划表
 *
 * @author chl
 * @date 2024-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/run/schedule")
public class ScheduleController extends BaseController {

    private final IScheduleService scheduleService;
    private final IRunListService runListService;
    private final IScoreService scoreService;
    /**
     * 查询阳光跑计划表列表
     */
    @SaCheckPermission("run:schedule:list")
    @GetMapping("/list")
    public TableDataInfo<ScheduleVo> list(ScheduleBo bo, PageQuery pageQuery) {
        return scheduleService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询阳光跑计划列表
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("run:schedule:query")
    @GetMapping("/infoList")
    public R<List<ScheduleVo>> getInfoList(ScheduleBo bo) {
        return R.ok(scheduleService.queryList(bo));
    }

    /**
     * 导出阳光跑计划表列表
     */
    @SaCheckPermission("run:schedule:export")
    @Log(title = "阳光跑计划表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ScheduleBo bo, HttpServletResponse response) {
        List<ScheduleVo> list = scheduleService.queryList(bo);
        ExcelUtil.exportExcel(list, "阳光跑计划表", ScheduleVo.class, response);
    }

    /**
     * 获取阳光跑计划表详细信息
     *
     * @param scheduleId 主键
     */
    @SaCheckPermission("run:schedule:query")
    @GetMapping("/{scheduleId}")
    public R<ScheduleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long scheduleId) {
        return R.ok(scheduleService.queryById(scheduleId));
    }

    /**
     * 新增阳光跑计划表
     */
    @SaCheckPermission("run:schedule:add")
    @Log(title = "阳光跑计划表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ScheduleBo bo) {
        Boolean scheduleResult = scheduleService.insertByBo(bo);
        if(!scheduleResult ){
            return R.fail("生成计划表失败");
        }
        return R.ok("生成计划表成功");
    }

    /**
     * 修改阳光跑计划表
     */
    @SaCheckPermission("run:schedule:edit")
    @Log(title = "阳光跑计划表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ScheduleBo bo) {
        return toAjax(scheduleService.updateByBo(bo));
    }

    /**
     * 删除阳光跑计划表
     *
     * @param scheduleIds 主键串
     */
    @SaCheckPermission("run:schedule:remove")
    @Log(title = "阳光跑计划表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scheduleIds}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] scheduleIds) {
        for (int i = 0; i < scheduleIds.length; i++) {
            ScheduleVo scheduleVo = scheduleService.queryById(scheduleIds[i]);
            RunListBo runListBo=new RunListBo();
            runListBo.setScheduleId(scheduleVo.getScheduleId().toString());
            runListBo.setTenantId(scheduleVo.getTenantId());
            Boolean aBoolean = runListService.deleteWithMemberId(runListBo);
            if (!aBoolean) {
                return R.fail("删除失败");
            }
            ScoreBo scoreBo=new ScoreBo();
            scoreBo.setTenantId(scheduleVo.getTenantId());
            scoreBo.setScheduleId(scheduleVo.getScheduleId().toString());
            Boolean aBoolean1 = scoreService.deleteWithBo(scoreBo);
            if (!aBoolean1) {
                return R.fail("删除失败");
            }
        }
        return toAjax(scheduleService.deleteWithValidByIds(List.of(scheduleIds), true));
    }
}
